package com.blb.service.impl;

import com.blb.entity.Dept;
import com.blb.entity.Post;
import com.blb.entity.User;
import com.blb.mapper.DeptMapper;
import com.blb.service.IDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.service.IPostService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoxiong
 * @since 2020-05-20
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {


    @Resource
    private DeptMapper deptMapper;

    /**
     * 上级部门添加
     * @param dept
     * @return
     */
    @Transactional
    @Override
     public String addDeptAll(Dept dept, Model model) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Post post = new Post();
        post.setDeptId(dept.getId());
        post.setPostId(dept.getId());
        post.setPostName(dept.getPostName());
        post.setCompanyId(user.getId());

        dept.setPostId(dept.getId());
        dept.setPid("0");
        dept.setCompanyId(user.getId());
        //判断部门id是否存在
        List<Dept> deptList = isDeptId(dept.getId(),user.getId());
        if(!deptList.isEmpty()){
            model.addAttribute("isExist","部门id已经存在");
            return "human/depatment";
        }else {
            List<Dept> depts = isDeptName(dept.getDeptName(),user.getId());
            if(!depts.isEmpty()){
                model.addAttribute("isExist","部门名称已经存在");
                return "human/depatment";
            }else{
                deptMapper.addDept(dept);
                deptMapper.addPost(post);
                return "human/depatment";
            }
        }

    }



    /**
     * 下级部门添加
     * @param dept
     * @return
     */
    @Override
    public int addLowDept(Dept dept) {
        return deptMapper.addLowDept(dept);
    }

    /**
     * 查询所有部门
     * @return
     */
    @Override
    public List<Dept> deptAll(String companyId) {
        return deptMapper.deptAll(companyId);
    }

    @Override
    public Dept selectById(String id,String companyId) {
        return deptMapper.selectById(id,companyId);
    }

    @Override
    public Integer  updById(Dept dept) {
        return deptMapper.updById(dept);
    }

    /**
     * 查询所有生产线
     * @return
     */
    @Override
    public List<Dept> queryProduction(String companyId) {
        return deptMapper.queryProduction(companyId);
    }

    @Override
    public List<Dept> isDeptId(String deptId, String companyId) {
        return deptMapper.isDeptId(deptId,companyId);
    }

    @Override
    public List<Dept> isDeptName(String deptName, String companyId) {
        return deptMapper.isDeptName(deptName,companyId);
    }

    @Transactional
    @Override
    public void delDept(String id) {
        removeById(id);
        deptMapper.delPost(id);
    }
}
